package com.base.io_test;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * create by yongli on 2020-05-04 23:27
 */

public class ClientMain {
    public static void main2(String[] args) throws Exception {

        Thread thread = new Thread(() -> req());
        thread.start();
        // 再开启一个socket
//        Thread thread1 = new Thread(() -> sub());
//        thread1.start();
    }

    public static void noInput(){
        try {
            Socket socket = new Socket("127.0.0.1", 7070);
            InputStream input = socket.getInputStream();
            byte[] buf = new byte[1024]; //
            int off;

            // 时刻等待客户端发来消息
            // 对面发一次，flush，read一次。
            while ((off = input.read(buf)) != -1) {
                String str = new String(buf);
                // 如果处理过慢，多次发的可能会合并到一次
                Thread.sleep(2000);
                System.out.println(str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("the socket client is over!");

    }


    public static void req() {
        try {
            System.out.println("I am from socket client!");
            Socket socket = new Socket("127.0.0.1", 7070);
            Thread.sleep(1000);
            OutputStream out = socket.getOutputStream();
            byte[] content = "you just need more practice, and you will get the destination".getBytes();
            out.write(content);
            out.flush();
            //out.close(); 会关闭socket


            InputStream input = socket.getInputStream();
            byte[] buf = new byte[1024];
            int off;
            // 这是一个阻塞io不会返回-1
            while ((off = input.read(buf)) != -1) {
                String str = new String(buf);
                System.out.println(str);
                System.out.println("the client");
                break; // 这个read方法什么手结束呢？
            }

            // 这里不能写了，对面关闭了连接
            Thread.sleep(200);
            System.out.println("next out");
            OutputStream out2 = socket.getOutputStream();
            byte[] content2 = "once，you just need more practice, and you will get the destination".getBytes();
            out2.write(content2);
            out2.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("the socket client is over!");
    }

    public static void sub() {
        try {
            Socket socket = new Socket("127.0.0.1", 7070);
            OutputStream out = socket.getOutputStream();
            byte[] content = "subscribe".getBytes();
            out.write(content);
            out.flush();
            //out.close(); 会关闭socket


            InputStream input = socket.getInputStream();
            byte[] buf = new byte[1024];
            int off;

            // 时刻等待客户端发来消息
            // 对面发一次，flush，read一次。
            while ((off = input.read(buf)) != -1) {
                String str = new String(buf);
                // 如果处理过慢，多次发的可能会合并到一次
                Thread.sleep(2000);
                System.out.println(str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("the socket client is over!");
    }

}
